<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Frontend Offers Interface (offers.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require_once(dirname(__FILE__)."/config.php");

function errorexit($head, $body) {
	$pagetitle = "Error";
	$pageheading = $head;
	$page_content = $body;
	require(TEMPLATE_DIR . "/index.php");
	exit;
}

// Set page title
$pagetitle = "Available Offers";

if(!isset($_GET['offer_code']) || $_GET['offer_code'] == "") {
	// Code not entered
	errorexit(OFFER_CODE . " Not Entered",
	"<p>We did not receive an" . OFFER_CODE ." with your request. " .
	"Please <a href=\"/\">try again</a>.</p>\n");
}

	// Offer code entered, attempt to get code info
$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX .
"campaigns` WHERE `campaign_name` = '" .
$db->escape($_GET['offer_code']) ."'");

if ($campaign == false) {
	// Code not found in database
	errorexit(OFFER_CODE . " Not Found", "<p>The " .
	OFFER_CODE . " you entered was not found. Please " .
	"<a href=\"/\">try again</a>.</p>\n");
}

if (time()+86400 < strtotime($campaign->start_date)) {
	// Offers have not posted yet.
	errorexit(OFFER_CODE . " Has Not Started Yet",
	"<p>The " . OFFER_CODE . " you entered has not started yet. ".
	"You may want to <a href=\"/\">try another " . OFFER_CODE .
	"</a>.</p>\n");
}

if (time()-86400 > strtotime($campaign->end_date)) {
	// Offers have expired.
	errorexit(OFFER_CODE . " Has Expired",
	"<p>The " . OFFER_CODE . " you entered has expired." .
	"Watch for the next " . ORGANIZATION . " advertisement in".
	"your local newspaper to get a newer " . OFFER_CODE .
	". Or, you may <a href=\"/\">try another " . OFFER_CODE .
	"</a>.</p>\n");
}

// Make attempt to get offers.
$offers = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offers` " .
"WHERE `campaign_id` = '". $campaign->campaign_id ."'");

if ($offers == false) {
	// No offers in campaign (shouldn't happen but let's catch it.)
	errorexit(OFFER_CODE . " Entered Has No Offers",
	"<p>The " . OFFER_CODE . " you entered does not have any offers " .
	"associated with it. Please contact " . ORGANIZATION . " to report" .
	"this message.</p>\n");
}

// Yay! Let's get the coupons out.
$pageheading = "Here Are Your " . SITE_NAME . " Special Offers";
$page_content = "<h2>For " . OFFER_CODE . " '" . $campaign->campaign_name . "'</h2>\n";
if(isset($_GET['printable'])) {	
	$page_content .= "<p>Format: <a href=\"offers.php?offer_code=" . $campaign->campaign_name . "\">Normal</a> | Printable</p>\n";
} else {
	$page_content .= "<p>Format: Normal | <a href=\"offers.php?offer_code=" . $campaign->campaign_name . "&amp;printable\">Printable</a></p>\n";
}
$page_content .= "<table id=\"offers\">\n";
$offerCount = 0;
foreach($offers as $offer) {
	if($offerCount % 2 == 0) $page_content .= "<tr>\n";
	$page_content .= "<td><img src=\"get_coupon.php?campaign_id=" .
	$campaign->campaign_id . "&amp;offer_id=" . $offer->offer_id .
	"\" alt=\"" . $offer->offer_line1 . " " . $offer->offer_line2 .
	" " . $offer->offer_line3 . "\" /></td>";
	$offerCount++;
	if($offerCount % 2 == 0 || $offerCount == $db->num_rows) $page_content .= "</tr>\n";
}
$page_content .= "</table>\n";
$page_content .= "<a href=\"/\">Return</a> to the " . SITE_NAME . " site.\n";

// Get template
require(TEMPLATE_DIR . "/index.php");
?>